package com.example.qywechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.qywechat.model.Department;
import com.example.qywechat.model.User;
import com.example.qywechat.util.HttpUtils;
import com.example.qywechat.util.QyWeChatUtil;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @author 王森
 */
@Controller
@CrossOrigin
public class TestController {

    @Autowired
    QyWeChatUtil qyWeChatUtil;

    // 测试请求参数
    @PostMapping("/test1")
    @ResponseBody
    public String test1( @RequestParam(value = "code") String code){


        return code;
    }
    // 获取浏览器
    @GetMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest request){
        String agent=request.getHeader("User-Agent");
        System.out.println(agent);
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        System.out.println(os.getName());
        System.out.println(browser.getName());

        return null;
    }



    @GetMapping("/getUserid")
    @ResponseBody
    public String getUserid(String code){
        System.out.println("code");
        System.out.println(code);
        String useridByCode = qyWeChatUtil.getUseridByCode(code);
        System.out.println(useridByCode);
//        String s = HttpUtils.sendGet("http://localhost:80/api/auth/oauth/token?grant_type=client_credentials&client_secret=web-secret&client_id=web");
//        System.out.println("返回结果"+s);
        JSONObject jsonObject = JSONObject.parseObject(useridByCode);
        String userId = (String) jsonObject.get("UserId");

        return userId;
    }
    @GetMapping("/getAccessToken")
    @ResponseBody
    public JSONObject getAccessToken(){
        JSONObject accessToken = qyWeChatUtil.getAccessToken();
        return accessToken;
    }

    @PostMapping("/createUser")
    @ResponseBody
    public ResponseEntity<String> create(User user){

        ResponseEntity<String> responseEntity = qyWeChatUtil.createUser(user);
        return responseEntity;
    }
    @PostMapping("/updateUser")
    @ResponseBody
    public ResponseEntity<String> updateUser(User user){

        ResponseEntity<String> responseEntity = qyWeChatUtil.updateUser(user);
        return responseEntity;
    }
    @GetMapping("/delectUser")
    @ResponseBody
    public ResponseEntity<String> delectUser(String userId){

        ResponseEntity<String> responseEntity = qyWeChatUtil.delectUser(userId);
        return responseEntity;
    }
    @GetMapping("/createDepartment")
    @ResponseBody
    public ResponseEntity<String> createDepartment(@RequestBody Department department){

//        Department department1 = new Department();
//        department1.setName("北京研发中心");
//        department1.setParentid("1");

        ResponseEntity<String> responseEntity = qyWeChatUtil.createDepartment(department);
        return responseEntity;
    }
    @GetMapping("/updateDepartment")
    @ResponseBody
    public ResponseEntity<String> updateDepartment(@RequestBody Department department){


        ResponseEntity<String> responseEntity = qyWeChatUtil.updateDepartment(department);
        return responseEntity;
    }
    @GetMapping("/delectDepartment")
    @ResponseBody
    public ResponseEntity<String> delectDepartment(String departmentId){

        ResponseEntity<String> responseEntity = qyWeChatUtil.delectDepartment(departmentId);
        return responseEntity;
    }
    @GetMapping("/t2")
    public String test02(){
        System.out.println("psotyes");
        return "psotyes";
    }

    public static void main(String[] args) {
        String str = "abcd,123,123abc,fij23,2,1";
        String[]  strs=str.split(",");
        for (String s:strs) {
            System.out.println(s);
        }
    }
}
